var id = GetQueryString("id");
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'client/capital/record?id='+id,
        datatype: "json",
        colModel: [			
			{ label: '现金流水ID', name: 'id', index: 'id', width: 100, key: true ,align:'center'},
			{ label: '借阅订单ID', name: 'borrowId', index: 'borrow_id', width: 100 ,align:'center'},
			{ label: '金额', name: 'chargeAmountCus', index: 'charge_amount', width: 80,align:'center',sortable:false }, 			
			{ label: '类型', name: 'chargeTypeCus', index: 'mobile_no', width: 80,align:'center' , 
				formatter:function(value,options,rowData){
					if(value==1){
						return "支付押金"
					}else if(value==2){
						return "借阅费"
					}else if(value==3){
						return "充值"
					}else if(value==4){
						return "活动充值"
					}else if(value==5){
						return "退还押金"
					}else if(value==6){
						return "充值券"
					}else if(value==7){
						return "VIP充值赠送"
					}else if(value==8){
						return "邀请赠送"
					}else if(value==9){
						return "活动赠送"
					}else{
						return "未知状态"
					}
			},sortable:false}, 			
			{ label: '项目', name: 'bookCount', index: 'deposit_amount', width: 80,align:'center',formatter:function(cellvalue, options, rowObject){
				if(rowObject.operType == 20||rowObject.operType == null){return rowObject.bookCount+"本"}else {return ""}
			},sortable:false }, 			
			{ label: '支付方式', name: 'chargeStyleCus', index: 'chargeStyleCus', width: 80,align:'center', 
				formatter:function(value,options,rowData){
					if(value==1){
						return "微信支付"
					}else if(value==2){
						return "支付宝支付"
					}else if(value==3){
						return "人工充值"
					}else if(value==4){
						return "余额支付"
					}else if(value==5){
						return "微信退款"
					}else if(value==6){
						return "支付宝退款"
					}else if(value==7){
						return "充值券"
					}else if(value==8){
						return "VIP充值赠送"
					}else if(value==9){
						return "邀请赠送"
					}else if(value==10){
						return "活动赠送"
					}else{
						return "未知状态"
					}
			},sortable:false }, 			
			{ label: '时间', name: 'createTime', index: 'create_time', width: 110,align:'center' }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userName: null,
			mobileNo: null,
			status:null,
			chargeStartTime:null,
			chargeEndTime:null,
			rufundStartTime:null,
			rufundEndTime:null
        },
		showList: true,
		title: null,
		capitalRecord: {}
	},
	mounted(){
	    this.init()
	},
	methods: {
		init:function(){
			var user_id = id;
			$.ajax({
				type: "POST",
			    url: baseURL + "client/capital/cashlog/",
			    contentType: "application/json",
			    data: JSON.stringify({"id":user_id}),
			    success: function(r){
			    	console.log(r);
			    	document.getElementById("expend").text=r.expendCount;
			    	document.getElementById("income").text=r.incomeCount;
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.marketBook = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.marketBook.id == null ? "marketbook/save" : "marketbook/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.marketBook),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "marketbook/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "marketbook/info/"+id, function(r){
                vm.marketBook = r.marketBook;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'userName' : vm.q.userName,
					'mobileNo' : vm.q.mobileNo,
					'status' : vm.q.status,
					'chargeStartTime':vm.q.chargeStartTime,
					'chargeEndTime':vm.q.chargeEndTime,
					'rufundStartTime':vm.q.rufundStartTime,
					'rufundEndTime':vm.q.rufundEndTime
				},
				page : page
			}).trigger("reloadGrid");
		}
	}
});